﻿@{
    Layout = null;
}
@model ElingOA6_Admin_DSJCJ.Model.PaperTopic
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <base target="_self" />
     @if (false)
   {
        <script src="../../../../Scripts/jquery-vsdoc.js" type="text/javascript"></script>
        <script src="../../../../Scripts/ASPxScriptIntelliSense.js" type="text/javascript"></script>
   }
    <link href="@Url.Content("~/Content/Site.css", true)" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/json2.js",true)" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.min.js", true)" type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/FloatDiv.js", true)" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/regex.js", true)" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/PageCommon.js", true)" type="text/javascript"></script>
    <script src="@Url.Content("~/Common/GetRootPath.ashx", true)" type="text/javascript"></script>

    @Html.DevExpress().GetStyleSheets(

        new StyleSheet { ExtensionType = ExtensionType.ListBox, Theme = DevExpressHelper.Theme }
)
    @Html.DevExpress().GetScripts(
        new Script { ExtensionType = ExtensionType.ListBox }
)

    <script type="text/javascript">

        $(document).ready(function () {
            var $tr = $(this).parent().parent();
            var nextAnswerNos = "";
            $tr.nextAll('tr').each(function () {
                var no1 = $(this).find('td').eq(2).html();
                nextAnswerNos += no1 + ",";
            });


            var hdMethod = $("#hdMethod").val();
            if (hdMethod == 'Edit') {
                var hdPaperTopicList = $("#hdPaperTopicList").val();
                if (hdPaperTopicList != "") {
                    var model = JSON.parse(hdPaperTopicList);
                    for (var z = 0; z < model.length; z++) {
                        var FIsRestsAnswer = model[z].FIsRestsAnswer == "Y" ? "自填" : "选择";
                        var FRestsAnswerCount = model[z].FIsRestsAnswer == "Y" ? "行数：" + model[z].FRestsAnswerCount : model[z].FAnswerContent;
                        $("#answerTable").append("<tr id ='Edit" + z + "' height=\"25\"><td width='50px' align='center'  class=\"tdClass\"><a href='#' onclick=cpDelete(this,'" + model[z].FPaperTopicId + "','" + model[z].FAnswerNo + "')><img class='delnyclass' src='" + rootPath + "/Content/images/delete.gif' style='cursor:pointer'/></a><input type=\"hidden\" value=\"" + model[z].FIsRestsAnswer + "\" /></td><td width='50px' align='center'  class=\"tdClass\" >" + FIsRestsAnswer + "</td><td width='50px' align='center' class=\"tdClass\">" + model[z].FAnswerNo + "</td><td class=\"tdClass\" style=\"font-size: 12px;\" align=\"left\">" + FRestsAnswerCount + "</td></tr>");

                    }
                }
            }
        });


        $(function () {
            if ($('#hidIsAnswer').val() == "1") {
                $('#btnOK').hide();
                $('#divEdit').css("display", "");
            }

            if ($('#hidJdt').val() == "1") {
                $('.jdt').show();
                $('#divAnswer').hide();
            }
            if ($('#hidbzdz').val() == "1") {
                $('#trbzda').show();
                ASPxClientComboBox.Cast("FTopicType").RemoveItem(2); //试卷有标准答案但无简答题（简答题无法自动计算是否正确）
                typeSelect = typeSelect.replace("<option value='1'>自填</option>", ""); //试卷选择项无自填
            }

            if ($('#hidAnswer').val() == "0") {
                $('#divAnswer').hide();
            }


            var editCount = parseInt($('#hidEditCount').val());
            if (editCount != 0) {
                t = editCount;
            }


            var topicJson = '{"FPaperId":0,"FTopic":null,"FTopicType":null,"FPaperTopicId":0,"FStdAnswer":null,"FShortAnswerCount":null,"FPubDate":null,"FPubDeptNo":null,"FPubEmpNo":null,"FPubIp":null,"FTitle":null,"FPaperClsNo":null,"FTopicTypeName":""}';
            var answerOptionJson = '{"FPaperTopicId":0,"FAnswerNo":null,"FAnswerContent":null,"FIsRestsAnswer":null,"FRestsAnswerCount":null}';
            $('#btnOK').click(function () {
                var topic = FTopic.GetText();
                if (topic == "") {
                    alert("请填写题目");
                    return;
                }

                var shortAnswerCount = 0;
                var topicType = FTopicType.GetValue();
                if (topicType == "A") {
                    shortAnswerCount = FShortAnswerCount.GetText();
                    if (shortAnswerCount == "") {
                        alert("请填写简答题行数");
                        return;
                    } else {
                        if (!isNumeric(shortAnswerCount)) {
                            alert("简答题行数必须是整数");
                            return;
                        }
                    }
                } else {
                    if ($('.topicAdd').length <= 0 && $("#answerTable").find('tr').length <= 2) {
                        alert("请设置选择答案");
                        return;
                    } else {
                        var isNotFillin = false;
                        var txtVal = "";
                        var noVal = "";
                        $('.topicAdd').each(function () {
                            txtVal = $(this).find('td').eq(3).find(':text').val();
                            noVal = $(this).find('td').eq(2).html();
                            if (txtVal == "") {
                                alert("请填写选择答案" + noVal + "的内容");
                                isNotFillin = true;
                                return false;
                            } else if ($(this).find('td').eq(1).find('select').val() == "1") {
                                if (!isNumeric(txtVal)) {
                                    alert("选择答案" + noVal + "的行数必须是整数");
                                    isNotFillin = true;
                                    return false;
                                }
                            }
                        });

                        if (isNotFillin) {
                            return;
                        }

                    }
                }

                var stdAnswer = "";
                if ($('#hidbzdz').val() == "1") {
                    stdAnswer = FStdAnswer.GetText();
                    if (stdAnswer == "") {
                        alert("请填写标准答案");
                        return;
                    } else {
                        var stdAnswerNos = "";
                        $('#answerTable').find('tr').each(function () {
                            stdAnswerNos += $(this).find('td').eq(2).html();
                        });
                        $('.topicAdd').each(function () {
                            stdAnswerNos += $(this).find('td').eq(2).html();
                        });
                        if (topicType == "R") //单选
                        {
                            if (stdAnswer.length > 1 || stdAnswerNos.indexOf(stdAnswer) == -1) {
                                alert("请填写正确的标准答案");
                                return;
                            }
                        }
                        else if (topicType == "C") //多选
                        {
                            var haveAnswerNo = "";
                            var isStdExists = true;
                            var stdAnswerArray = stdAnswer.split('');
                            for (var i = 0; i < stdAnswerArray.length; i++) {
                                if (haveAnswerNo.indexOf(stdAnswerArray[i]) != -1) {
                                    isStdExists = false;
                                    break;
                                } else {
                                    haveAnswerNo += stdAnswerArray[i];
                                }
                                if (isStdExists) {
                                    if (stdAnswerNos.indexOf(stdAnswerArray[i]) == -1) {
                                        isStdExists = false;
                                        break;
                                    }
                                }
                            }

                            if (!isStdExists) {
                                alert("请填写正确的标准答案");
                                return;
                            }

                        }
                    }
                }


                var topicModel = JSON.parse(topicJson);
                var answerList = JSON.parse("[]");

                topicModel.FPaperId = $('#hidPaperID').val();
                topicModel.FTopic = topic;
                topicModel.FTopicType = topicType;
                topicModel.FPaperTopicId = $('#hidPaperTopicID').val();
                topicModel.FStdAnswer = stdAnswer;
                topicModel.FShortAnswerCount = shortAnswerCount;

                if (topicType != "A") //不是简答题
                {
                    $('.topicAdd').each(function () {
                        var answerOptionModel = JSON.parse(answerOptionJson);
                        answerOptionModel.FPaperTopicId = $('#hidPaperTopicID').val();
                        answerOptionModel.FAnswerNo = $(this).find('td').eq(2).html();

                        if ($(this).find('td').eq(1).find('select').val() == "1") {
                            answerOptionModel.FAnswerContent = "";
                            answerOptionModel.FIsRestsAnswer = "Y";
                            answerOptionModel.FRestsAnswerCount = $(this).find('td').eq(3).find(':text').val();
                        } else {
                            answerOptionModel.FAnswerContent = $(this).find('td').eq(3).find(':text').val();
                            answerOptionModel.FIsRestsAnswer = "N";
                            answerOptionModel.FRestsAnswerCount = "0";
                        }

                        answerList.push(answerOptionModel);

                    });
                }


                var topicModelJson = JSON.stringify(topicModel);
                var answerOptionListJson = "";
                if (answerList.length > 0) {
                    answerOptionListJson = JSON.stringify(answerList);
                }

                if (topicModel.FPaperTopicId == 0) //增加
                {
                    $.ajax({
                        type: 'POST',
                        url: rootPath + "/Admin/PaperTopic/PaperTopicAdd",
                        data: { topic: topicModelJson, answerOption: answerOptionListJson },
                        success: function (result) {
                            var ok = result.split(",");
                            if (ok[0] == "ok") {
                                alert("增加保存成功");
                                window.returnValue = "ok";
                                window.close();
                            } else {
                                alert("增加保存失败");
                            }
                        }
                    });

                }
                else //修改
                {
                    $.ajax({
                        type: 'POST',
                        url: rootPath + "/Admin/PaperTopic/PaperTopicEdit",
                        data: { topic: topicModelJson, answerOption: answerOptionListJson },
                        success: function (result) {
                            if (result == "ok") {
                                alert("修改保存成功");
                                window.returnValue = "ok";
                                window.close();
                            } else {
                                alert("修改保存失败");
                            }
                        }
                    });


                }

            });


        });




        var noArray = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"];
        var noStr = "ABCDEFGHIJKLMNOP";
        var arrayLength = noArray.length;
        var t = 0;
        var typeSelect = "<select onchange='topicTypeChange(this);'><option value='0'>选择</option><option value='1'>自填</option></select>";
        var imgHtml = "<img src='" + rootPath + "/Content/images/delete.gif' style='cursor:pointer'  onclick='rowDel(this);'/>";
        var txtHtml = "<input type='text' style='width:96%'  />";
        var jdtTxtHtml = "行数：<input type='text' style='width:100px' value='1'  />";
         function addTopic() {
            if (t >= arrayLength) {
                alert("超出题目最大量");
            } else {
                $('#answerTable').append("<tr class='topicAdd'><td width='50px' align='center' class=\"tdClass\">" + imgHtml + "</td><td width='50px' align='center'  class=\"tdClass\">" + typeSelect + "</td><td width='50px' align='center'  class=\"tdClass\">" + noArray[t] + "</td><td  class=\"tdClass\">" + txtHtml + "</td></tr>");
                t++;
            }
        }

        
        function rowDel(obj) {
            var $tr = $(obj).parent().parent();
            $tr.nextAll('tr').each(function () {
                var no = $(this).find('td').eq(2).html();
                var noIndex = noStr.indexOf(no);
                $(this).find('td').eq(2).html(noArray[noIndex - 1]);
            });

            updStdAnswer($tr.find('td').eq(2).html());
            $tr.remove();
            t--;       
        }



        function topicTypeChange(obj) {
            var val = $(obj).val();
            var $tr = $(obj).parent().parent();
            if (val == "0") {
                $tr.find('td').eq(3).html(txtHtml);
            } else if (val == "1") {

                //判断是否已有自填题
                var zt = 0;
                
                $('.delnyclass').each(function () {
                    if ($(this).parent().parent().parent().find('td').eq(0).find(':hidden').val() == "Y") {
                        zt = 1;                
                        return false;
                    }
                });

                if (zt == 0) {
                    zt = -1;
                    $('.topicAdd').each(function () {
                        if ($(this).find('td').eq(1).find('select').val() == "1") {
                            zt++;
                        }
                    });

                }
                       
                if (zt == 0) {
                    $tr.find('td').eq(3).html(jdtTxtHtml);
                } else {
                    alert("一道题目最多只能有一个自填选项");
                    $(obj).val('0');
                }

            }

        }

        
        function updStdAnswer(answerNo) {
            if ($('#hidbzdz').val() == "1") {
                stdAnswer = FStdAnswer.GetText();
                $('#FStdAnswer').val(stdAnswer.replace(answerNo, ""));
            }
        }


        function cpDelete(obj, topicID, answerNo) {
            var $tr = $(obj).parent().parent();


            var nextAnswerNos = "";
            $tr.nextAll('tr').each(function () {
                var no1 = $(this).find('td').eq(2).html();
               
                nextAnswerNos += no1 + ",";
            });

            if (window.confirm("确定删除")) {
                $.ajax({
                    type: 'POST',
                    url: rootPath + '/Admin/PaperTopic/DelAnswerOption',
                    data: { topicID: topicID, answerNo: answerNo, nextAnswerNos: nextAnswerNos },
                    success: function (result) {
                        if (result == "ok") {
                            alert("删除成功!");

                            $tr.nextAll('tr').each(function () {
                                var no = $(this).find('td').eq(2).html();
                                var noIndex = noStr.indexOf(no);
                                $(this).find('td').eq(2).html(noArray[noIndex - 1]);
                            });

                            $('.topicAdd').each(function () {
                                var no = $(this).find('td').eq(2).html();
                                var noIndex = noStr.indexOf(no);
                                $(this).find('td').eq(2).html(noArray[noIndex]);
                            });


                            updStdAnswer($tr.find('td').eq(2).html());
                            
                            $tr.remove();
                            t--;
                        } else {
                            alert("删除失败");
                        }
                    }
                });
            }
        
        
         }


         function valueChanged(s, e) {
             var clientFTopicTypeObj = ASPxClientComboBox.Cast("FTopicType");
             if (clientFTopicTypeObj.GetValue() == "A") {
                 $('.jdt').show();
                 $('#divAnswer').hide();
             }
             else {
                 $('.jdt').hide();
                 $('#divAnswer').show();

             }

         }


    </script>

</head>
<body>
   @using (Html.BeginForm("Save", "PaperTopic", FormMethod.Post, new { id = "form1" }))
   {
    <div class="div">
        <table width="99%" align="center">            
            <tr>
                <th width="15%"  class="tdClass">问卷主题:</th>
                <td align="left" colspan="3" class="tdClass">
                 @Html.DevExpress().TextBox(settings =>
               {
                   settings.Name = "FTitle";
                   settings.Width = System.Web.UI.WebControls.Unit.Percentage(96);
                   settings.Text = ViewData["title"].ToString();
                   settings.Enabled = false;
                   settings.Properties.ValidationSettings.EnableCustomValidation = true;
               }).GetHtml()
                      
                </td>               
            </tr>
            <tr>
                <th width="15%"  class="tdClass">题目:</th>
                <td align="left" colspan="3" class="tdClass">
                @Html.DevExpress().Memo(settings =>
               {
                   settings.Name = "FTopic";
                   settings.Width = System.Web.UI.WebControls.Unit.Percentage(96);
                   settings.Text = ViewData["topic"].ToString();
                   settings.Properties.ValidationSettings.EnableCustomValidation = true;
                   settings.Properties.Rows = 3;
               }).GetHtml()
                </td>               
            </tr>           
            <tr>
                <th width="15%" class="tdClass">题目类型:</th>
                <td width="35%" align="left" class="tdClass">
                 @Html.DevExpress().ComboBox(settings =>
                 {
                     settings.Name = "FTopicType";
                     settings.Properties.TextField = "Text";
                     settings.Properties.ValueField = "Value";
                     settings.Properties.DropDownStyle = DropDownStyle.DropDownList;
                     settings.Enabled = true;
                     settings.Properties.EnableAnimation = false;
                     settings.Properties.ClientSideEvents.ValueChanged = "function(s,e){valueChanged(s,e)}";
                 }).BindList(new List<SelectListItem> 
                        { 
                            new SelectListItem { Text = "单选题", Value = "R" },
                            new SelectListItem { Text = "多选题", Value = "C" },
                            new SelectListItem { Text = "简答题", Value = "A" }
                        }).Bind(Model.FTopicType).GetHtml()
                </td>
                <th width="15%" class="tdClass">&nbsp;<span class="jdt" style="display:none">简答题行数:</span></th>
                <td width="35%" align="left" class="tdClass">
                <span class="jdt" style="display:none" >
                  @Html.DevExpress().TextBox(settings =>
               {
                   settings.Name = "FShortAnswerCount";
                   settings.Width = System.Web.UI.WebControls.Unit.Percentage(96);
                   settings.Text = ViewData["shortAnswerCount"].ToString();                 
                   //settings.Text = Model.FShortAnswerCount.ToString();
                   settings.Properties.ValidationSettings.EnableCustomValidation = true;
               }).GetHtml()
               </span>               
                </td>
            </tr>
            <tr id="trbzda"  style="display:none">
                <th width="15%" class="tdClass">标准答案:</th>
                <td align="left" colspan="3" class="tdClass">
                     @Html.DevExpress().TextBox(settings =>
               {
                   settings.Name = "FStdAnswer";
                   settings.Width = System.Web.UI.WebControls.Unit.Percentage(96);
                   settings.Text = Model.FStdAnswer;
                   settings.Properties.ValidationSettings.EnableCustomValidation = true;
               }).GetHtml()       
                </td>                
            </tr>
                 
        </table>
    </div>

    <div id="divAnswer">
      <fieldset style="width:99%;" align="center">
            <legend align="left">@Html.DevExpress().Label(settings => { settings.Text = "选择答案"; }).GetHtml() <img src="@Url.Content("~/Content/images/add.gif")" style="cursor:pointer"  onclick="addTopic();"/></legend>
            <table id="answerTable" width="99%" align="center" cellpadding="0" class="tableForm"> 
                  <tr>
                        <th width="5%" class="td_text" style =" text-align:center">@Html.DevExpress().Label(settings => { settings.Text = "操作"; }).GetHtml()</th>
                        <th width="7%" class="td_text" style =" text-align:center">@Html.DevExpress().Label(settings => { settings.Text = "类型"; }).GetHtml()</th>
                        <th width="5%" class="td_text" style =" text-align:center">@Html.DevExpress().Label(settings => { settings.Text = "编号"; }).GetHtml()</th>                     
                        <th width="83%" class="td_text" style =" text-align:center">@Html.DevExpress().Label(settings => { settings.Text = "内容"; }).GetHtml()</th>                     
                 </tr>
                 <tr id="trAnswerEdit" runat="server"  style="display:none">
                 </tr>
            </table>
      </fieldset>
    </div>  
    
    <br />
    <div class="div">
        <table width="100%">
            <tr><td align="center"><input type="button" id="btnOK" class="btn" value="确定" />&nbsp;<input type="button" id="btnCancel" class="btn" value="取消" onclick="javascript:window.close();" /></td></tr>        
        </table>
    </div>


    <div class="div" id="divEdit"  style="text-align:center; color:Red; display:none;">提示：该题目已有答题存在，无法再进行修改！</div> 

    @Html.Hidden("hidbzdz", ViewData["hidbzdz"]);
    @Html.Hidden("hidJdt", ViewData["hidJdt"]);
    @Html.Hidden("hidIsAnswer", ViewData["hidIsAnswer"]);
    @Html.Hidden("hidPaperID", ViewData["hidPaperID"]);
    @Html.Hidden("hidPaperTopicID", ViewData["hidPaperTopicID"]);
    @Html.Hidden("hidEditCount", ViewData["hidEditCount"]);
    @Html.Hidden("hidMessage", ViewData["message"]);
    @Html.Hidden("hidIsClose", ViewData["IsClose"])
    @Html.Hidden("hdMethod", ViewData["hdMethod"]);
    @Html.Hidden("hdPaperTopicList", (ViewData["PaperTopicList"]));                                                                                                                                                                                                                                                                                                                                                          
   }
</body>
</html>
